package com.ztgf.commodity.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.commodity.common.config.YmlConfig;
import com.ztgf.commodity.mapper.CategoryMaintainRecordMapper;
import com.ztgf.commodity.models.dto.*;
import com.ztgf.commodity.models.service.*;
import com.ztgf.commodity.models.vo.CategoryMaintainRecordVO;
import com.ztgf.commodity.models.vo.CategoryParamBean;
import com.ztgf.commodity.models.vo.CommoditySpecificationVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.ztgf.common.utils.UtilConstants.isNull;

@Slf4j
@DubboService
public class CategoryMaintainRecordServiceImpl extends ServiceImpl<CategoryMaintainRecordMapper, CategoryMaintainRecordDTO> implements ICategoryMaintainRecordService {

    @Resource
    private YmlConfig ymlConfig;
    @DubboReference
    private ICommodityCategoryService commodityCategoryService;
    @DubboReference
    private ICommoditySpecificationService commoditySpecificationService;
    @DubboReference
    private ICommoditySpecificationAttrService commoditySpecificationAttrService;
    @DubboReference
    private ICommodityParamService commodityParamService;
    @DubboReference
    private ICommodityParamValueService commodityParamValueService;
    @DubboReference
    private ICommodityUnitService commodityUnitService;
    @DubboReference
    private ICommodityLabelService commodityLabelService;
    @DubboReference
    private IBrandInfoService brandInfoService;

    @Override
    public Page<CategoryMaintainRecordVO> queryPageList(Integer pageNo, Integer pageSize) {
        Wrapper<CategoryMaintainRecordDTO> wrapper = new EntityWrapper<>();
        wrapper.orderBy("create_time", false);
        Page<CategoryMaintainRecordDTO> page = new Page<>(pageNo, pageSize);
        page = this.selectPage(page, wrapper);
        List<CategoryMaintainRecordDTO> list = page.getRecords();
        if (isNull(list)) {
            return null;
        }
        Page<CategoryMaintainRecordVO> result = new Page<>();
        List<CategoryMaintainRecordVO> listA = new ArrayList<>();
        for (CategoryMaintainRecordDTO dto : list) {
            // 查询一级类目
            CommodityCategoryDTO categoryA = commodityCategoryService.selectById(dto.getFirstCategoryId());
            // 查询二级类目
            CommodityCategoryDTO categoryB = commodityCategoryService.selectById(dto.getTwoCategoryId());
            // 查询三级类目
            CommodityCategoryDTO categoryC = commodityCategoryService.selectById(dto.getThreeCategoryId());
            // 查询品牌
            BrandInfoDTO brand = brandInfoService.selectById(dto.getBrandId());
            // 销售属性
            List<CommoditySpecificationVO> listC = new ArrayList<>();
            if (StringUtils.isNotBlank(dto.getSpecificationIds())) {
                String[] ids = dto.getSpecificationIds().split(",");
                for (String id : ids) {
                    CommoditySpecificationDTO selectA = commoditySpecificationService.selectById(id);
                    if (selectA != null) {
                        // 查询规格属性值
                        Wrapper<CommoditySpecificationAttrDTO> wrapperA = new EntityWrapper<>();
                        wrapperA.eq("specification_id", selectA.getId());
                        List<CommoditySpecificationAttrDTO> listB = commoditySpecificationAttrService.selectList(wrapperA);
                        CommoditySpecificationVO voA = new CommoditySpecificationVO();
                        voA.setId(selectA.getId());
                        voA.setName(selectA.getName());
                        if (!isNull(listB)) {
                            voA.setAttrList(listB);
                        }
                        listC.add(voA);
                    }
                }
            }
            // 参数属性
            List<CategoryParamBean> listD = new ArrayList<>();
            if (StringUtils.isNotBlank(dto.getParamIds())) {
                String[] ids = dto.getParamIds().split(",");
                for (String id : ids) {
                    CommodityParamDTO selectB = commodityParamService.selectById(id);
                    if (selectB != null) {
                        // 查询参数值
                        Wrapper<CommodityParamValueDTO> wrapperB = new EntityWrapper<>();
                        wrapperB.eq("param_id", selectB.getId());
                        List<CommodityParamValueDTO> selectList = commodityParamValueService.selectList(wrapperB);
                        CategoryParamBean bean = new CategoryParamBean();
                        bean.setId(selectB.getId());
                        bean.setParamName(selectB.getParamName());
                        if (!isNull(selectList)) {
                            bean.setValueList(selectList);
                        }
                        listD.add(bean);
                    }
                }
            }
            // 单位属性
            List<CommodityUnitDTO> listE = new ArrayList<>();
            if (StringUtils.isNotBlank(dto.getUnitIds())) {
                String[] ids = dto.getUnitIds().split(",");
                for (String id : ids) {
                    CommodityUnitDTO select = commodityUnitService.selectById(id);
                    if (select != null) {
                        listE.add(select);
                    }
                }
            }
            // 标签属性
            List<CommodityLabelDTO> listF = new ArrayList<>();
            if (StringUtils.isNotBlank(dto.getLabelIds())) {
                String[] ids = dto.getLabelIds().split(",");
                for (String id : ids) {
                    CommodityLabelDTO select = commodityLabelService.selectById(id);
                    if (select != null) {
                        listF.add(select);
                    }
                }
            }
            CategoryMaintainRecordVO vo = new CategoryMaintainRecordVO();
            vo.setId(dto.getId());
            if (categoryA != null) {
                vo.setOneCategory(categoryA);
            }
            if (categoryB != null) {
                vo.setTwoCategory(categoryB);
            }
            if (categoryC != null) {
                vo.setSCategory(categoryC);
            }
            if (brand != null) {
                vo.setBrandInfo(brand);
            }
            if (!isNull(listC)) {
                vo.setSalesAttributeCollection(listC);
            }
            if (!isNull(listD)) {
                vo.setParameterAttributeCollection(listD);
            }
            if (!isNull(listE)) {
                vo.setUnitCollection(listE);
            }
            if (!isNull(listF)) {
                vo.setLabelCollection(listF);
            }
            vo.setCreateTime(dto.getCreateTime());
            listA.add(vo);
        }
        result.setRecords(listA);
        result.setTotal(page.getTotal());
        result.setSize(page.getSize());
        result.setCurrent(page.getCurrent());
        return result;
    }
}
